%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% calculates error between experimental and fitted diffraction
% pattern according to RMS error as explained in
% J. Miao et al "Phase retrieval from the magnitude of the Fourier transforms of nonperiodic objects" 
% J. Opt. Soc. Am. A/ Vol. 15, No. 6/June 1998
% object - amplitude of the reconstructed object
% support
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [iterative_error] = error_function_Miao(object, support)

[Nx Ny] = size(support);

a = 0;
b = 0;

for ii = 1:Nx
    for jj = 1:Ny
if ((support(ii,jj)>0))
a = a + object(ii,jj)*object(ii,jj);
else
b = b + object(ii,jj)*object(ii,jj);
end
    end
end

iterative_error = sqrt(b/a);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%